package service;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import com.googlecode.objectify.Query;

import model.News;


public class Util {
	public static String convertAlias(String str)
	{
		char[] SPECIAL_CHARACTERS = { ' ', '!', '"', '#', '$', '%',
                '*', '+', ',', ':', '<', '=', '>', '?', '@', '[', '\\', ']', '^',
                '`', '|', '~', 'À', 'Á', 'Â', 'Ã', 'È', 'É', 'Ê', 'Ì', 'Í', 'Ò',
                'Ó', 'Ô', 'Õ', 'Ù', 'Ú', 'Ý', 'à', 'á', 'â', 'ã', 'è', 'é', 'ê',
                'ì', 'í', 'ò', 'ó', 'ô', 'õ', 'ù', 'ú', 'ý', 'Ă', 'ă', 'Đ', 'đ',
                'Ĩ', 'ĩ', 'Ũ', 'ũ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ạ', 'ạ', 'Ả', 'ả', 'Ấ',
                'ấ', 'Ầ', 'ầ', 'Ẩ', 'ẩ', 'Ẫ', 'ẫ', 'Ậ', 'ậ', 'Ắ', 'ắ', 'Ằ', 'ằ',
                'Ẳ', 'ẳ', 'Ẵ', 'ẵ', 'Ặ', 'ặ', 'Ẹ', 'ẹ', 'Ẻ', 'ẻ', 'Ẽ', 'ẽ', 'Ế',
                'ế', 'Ề', 'ề', 'Ể', 'ể', 'Ễ', 'ễ', 'Ệ', 'ệ', 'Ỉ', 'ỉ', 'Ị', 'ị',
                'Ọ', 'ọ', 'Ỏ', 'ỏ', 'Ố', 'ố', 'Ồ', 'ồ', 'Ổ', 'ổ', 'Ỗ', 'ỗ', 'Ộ',
                'ộ', 'Ớ', 'ớ', 'Ờ', 'ờ', 'Ở', 'ở', 'Ỡ', 'ỡ', 'Ợ', 'ợ', 'Ụ', 'ụ',
                'Ủ', 'ủ', 'Ứ', 'ứ', 'Ừ', 'ừ', 'Ử', 'ử', 'Ữ', 'ữ', 'Ự', 'ự', };
		
		char[] REPLACEMENTS = { '-', '\0', '\0', '\0', '\0', '\0',
                '\0', '_', '\0', '_', '\0', '\0', '\0', '\0', '\0', '\0', '_',
                '\0', '\0', '\0', '\0', '\0', 'A', 'A', 'A', 'A', 'E', 'E', 'E',
                'I', 'I', 'O', 'O', 'O', 'O', 'U', 'U', 'Y', 'a', 'a', 'a', 'a',
                'e', 'e', 'e', 'i', 'i', 'o', 'o', 'o', 'o', 'u', 'u', 'y', 'A',
                'a', 'D', 'd', 'I', 'i', 'U', 'u', 'O', 'o', 'U', 'u', 'A', 'a',
                'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A',
                'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'E', 'e', 'E', 'e',
                'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'I',
                'i', 'I', 'i', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o',
                'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O',
                'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u',
                'U', 'u', };
		
		for (int i = 0; i < REPLACEMENTS.length; i++) {
			str = str.replace(SPECIAL_CHARACTERS[i], REPLACEMENTS[i]);
		}
		str = str.toLowerCase();
		str = str.replaceAll("[^a-z0-9]+", "-").replaceAll("[-]+$", "");
		
		return str;
	}
	
	public static String keyword(String str)
	{
		String string  = "";
		str = str.trim();
		str = str.replaceAll("[ ]*[.]+[ ]*", "daucham");
		str = str.replaceAll("[ ]+", "-");
		str = str.replaceAll("[\\,\\=\\+.!@#$%^&*()_]+", ".-").replace("“", ".").replace("”", ".");
		str = str.replaceAll("daucham", ".");
		
		String[] words = str.split("-");
		int count = 0;
		String check = "";
		String[] check_arr = new String[5];
		String s_word = "";
		String s_check = "";

		for(int i=0;i<(words.length-1);i++)
		{
			if(Pattern.matches("^[A-ZĐÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝĂĨŨƠƯẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼẾỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪỬỮỰ]+[\\w\\W]+", words[i]) && count <=4&& i != 0 &&!Pattern.matches("^[^.]+[.]+[\\w\\W]*", words[i]))
			{
				String wk = "";
				if(i<(words.length-2)&&Pattern.matches("^[A-ZĐÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝĂĨŨƠƯẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼẾỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪỬỮỰ]+[\\w\\W]+", words[i+1])&&!Pattern.matches("^[^.]+[.]+[\\w\\W]+", words[i+1])&&!Pattern.matches("^[^.]+[.]+[\\w\\W]*", words[i]))
				{
					words[i] = words[i]+" "+words[i+1];
					wk = words[i]; 
					i=i+1;
				}
				if(i<(words.length-2)&&Pattern.matches("^[A-ZĐÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝĂĨŨƠƯẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼẾỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪỬỮỰ]+[\\w\\W]+", words[i+1])&&!Pattern.matches("^[^.]+[.]+[\\w\\W]+", words[i+1])&&!Pattern.matches("^[^.]+[.]+[\\w\\W]*", words[i]))
				{
					words[i] = words[i-1]+" "+words[i+1];
					wk = words[i];
					i=i+1;
				}
				
				if(i<(words.length-2)&&Pattern.matches("^[A-ZĐÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝĂĨŨƠƯẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼẾỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪỬỮỰ]+[\\w\\W]+", words[i+1])&&!Pattern.matches("^[^.]+[.]+[\\w\\W]+", words[i+1])&&!Pattern.matches("^[^.]+[.]+[\\w\\W]*", words[i]))
				{
					words[i] = words[i-1]+" "+words[i+1];
					wk = words[i];
					i=i+1;
				}
				
				check = "";
				if(!wk.equals(""))
					s_word = wk;
				else
					s_word = words[i];
				for(int j=0;j<check_arr.length;j++)
				{
					if(check_arr[j] != null)
						s_check = check_arr[j].toLowerCase();
					if(s_word.toLowerCase().equals(s_check))
					{
						check = "1";
					}
				}
				if(checkRemove(s_word))
					check = "1";
				
				if(check.equals("")&&s_word.length()>=5)
				{
					if(count == 0)
					{
						string += s_word.replaceAll("[\\,\\=\\+ .!@#$%^&*()_-]+", " ").replace("“", "").trim();
					} else {
						string += ","+s_word.replaceAll("[\\,\\=\\+ .!@#$%^&*()_-]+", " ").replace("“", "").trim();
					}
					check_arr[count] = s_word;
					count++;
					
				}
			}
			if(count >=6)
			{
				break;
			}
		}
		
		return string;
	}
	
	public static boolean checkRemove(String str)
	{
		boolean boo = false;
		str = str.toLowerCase();
		
		String remove = "theo,Nhưng,Vì,Có,Các,Vì thế,Chính vì thế,Thậm chí,Tuy nhiên,Vì vậy,Nhiều,Đừng,Tùy,Hiện nay,Tuỳ theo,Tuy vậy,sau,Nay,Thông,Thế mà,Bữa nay,Kế đến,Người,Chẳng,Trong,Chuyện,Không,Những,Chiếc,trước,source,chuyến,thường,nguồn,chúng";
		
		String[] listRemove = remove.split(",");
		
		for (int i = 0; i < listRemove.length; i++) {
			if(str.startsWith(listRemove[i].toLowerCase()))
				boo = true;
		}
		if(str.equals("Source"))
		{
			boo = true;
		}
		
		return boo;
	}
	
	public static String cutDescription(String str)
	{
		String des = "";
		str = str.replaceAll("\\<.*?\\>", "").trim();
		if(str.length()>250)
			des = str.substring(0, 250);
		des = des.replaceAll("[ ]+[^ ]*$", " ...");
		
		return des;
	}
	public static String cleanString(String str)
	{
		str = str.replace("\"", "");
		str = str.replace("\'", "");
		return str;
	}
	public static String cleanContent(String str)
	{
		str = str.replaceAll("width[ ]*:[ ]*[0-9a-zA-Z ]+[;]*", "");
		return str;
	}
	public static ArrayList<News> convertArrayList(Query<News> news)
	{
		ArrayList<News> arrayNews = new ArrayList<News>();
		for(News newsO : news)
		{
			arrayNews.add(newsO);
		}
		return arrayNews;
	}
	public static ArrayList<String> awayArrayList(ArrayList<String> list)
	{
		ArrayList<String> arrayList = new ArrayList<String>();
		for(int i=(list.size()-1);i>=0;i--)
		{
			arrayList.add(list.get(i));
		}
		return arrayList;
	}
	public static String GetHtml(String link,String charset){
		String content="";
		
		URL url;
		try {
			url = new URL(link);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		    connection.setInstanceFollowRedirects(true);
		    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Keep-Alive", "115");
			connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
			connection.setRequestProperty("Cache-Control", "max-age=0");
			BufferedReader reader=null;
			if("gzip".equals(connection.getContentEncoding()))
		    {
				reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream()), Charset.forName(charset)));
		    } else {
		    	reader= new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName(charset)));
		    }
			char[] buf = new char[102102];
			int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return content;
	}
}
